package com.tuo51.childcare.controller;


import com.tuo51.evaluate.domain.news.Article;
import com.tuo51.core.domain.staffs.Staff;
import com.tuo51.evaluate.service.article.ArticleCategoryService;
import com.tuo51.evaluate.service.article.ArticleService;
import com.tuo51.foudation.model.MemVerificationCache;
import com.tuo51.foudation.model.StatusResponse;
import com.tuo51.foudation.query.base.PagerQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.tuo51.foudation.constant.RequestConstant.APP_CHANNEL;
import static com.tuo51.foudation.constant.RequestConstant.TICKET;

@RestController
@RequestMapping("/papi/news")
@Api(value = "/papi/news", tags = "家长服务号 - 新闻列表（运营位）")
public class PAPINewsController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private ArticleCategoryService categoryService;

    @Autowired
    private MemVerificationCache cache;

    @GetMapping(value = "/article/{articleId}",produces = "text/html;charset=utf-8")
    @ApiOperation("查看文章")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "articleId", value = "文章id", required = true),
            @ApiImplicitParam(name = "ticket", value = "票据", paramType = "header", required = false)

    })
    public String articleDetail(@RequestHeader(value = "ticket", required = false) String ticket,
                                  @PathVariable("articleId") String articleId) {
        Article article = articleService.articleDetail(articleId);

        return article.getContent();
    }

    @GetMapping(value = "/categories")
    public StatusResponse categoryList(@RequestHeader(TICKET) String ticket,
                                       @RequestHeader(value = APP_CHANNEL, required = false) String channel ){

        Staff staff = cache.getStaff(ticket);

        return StatusResponse.success(categoryService.getAllCategories(channel));
    }

    @GetMapping(value = "/list")
    public StatusResponse newsList(@RequestHeader(TICKET) String ticket,
                                   @RequestHeader(value = APP_CHANNEL, required = false) String channel,
                                   String category,
                                   String categoryId,
                                   String staticCode,
                                   String keyword,
                                   PagerQuery pagerQuery){
        if (pagerQuery.getPageSize() <= 0){
            pagerQuery.setPageSize(10);
        }

        Staff staff = cache.getStaff(ticket);
        if (StringUtils.isEmpty(channel)){
            channel = "mp_public"; //hot fix for
        }
        return StatusResponse.success(articleService.newsList(false, channel, categoryId, category, staticCode, keyword, pagerQuery));
    }


    @GetMapping(value = "/recommend_list")
    public StatusResponse recommendNewsList(@RequestHeader(TICKET) String ticket,
                                            @RequestParam List<String> objectIds,
                                            @RequestParam Integer objectType,
                                            PagerQuery pagerQuery){
        if (pagerQuery.getPageSize() <= 0){
            pagerQuery.setPageSize(10);
        }

        Staff staff = cache.getStaff(ticket);

        return StatusResponse.success(articleService.getArticleItemsByObject(objectIds, objectType, null));
    }

}
